/*
 * IMwindowGui.java
 *
 * Created on March 27, 2008, 11:48 PM
 */

package imessenger;

import java.awt.Toolkit;
import java.io.IOException;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
 *
 * @author  Shelley Jane C. Co
 */
public class IMchatGui extends javax.swing.JFrame {
    
    private IMClient imclient;
    private String receiver;
    private String sessionHost = "";
    
    /** Creates new form IMwindowGui */
    public IMchatGui(IMClient client)
    {
        this.imclient = client;
        this.sessionHost = client.getSessionHost();
        initComponents();
        
        KeyStroke ks = KeyStroke.getKeyStroke("ENTER");
        sendTxtArea.getInputMap().put(ks, "none");
        
        KeyListener keyListener = new KeyAdapter()
        {
            public void keyPressed(KeyEvent e)
            {
                if(e.getKeyCode() == 10)    {
                    doSendMessage();
                }
            }
        };
        this.sendTxtArea.addKeyListener(keyListener);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        chatTxtArea = new javax.swing.JTextArea();
        sendPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        sendTxtArea = new javax.swing.JTextArea();
        sendBtn = new javax.swing.JButton();
        buzzBtn = new javax.swing.JButton();
        statusLbl = new javax.swing.JLabel();

        setResizable(false);

        chatTxtArea.setColumns(20);
        chatTxtArea.setEditable(false);
        chatTxtArea.setRows(5);
        chatTxtArea.setRequestFocusEnabled(false);
        jScrollPane2.setViewportView(chatTxtArea);

        sendTxtArea.setColumns(20);
        sendTxtArea.setRows(3);
        sendTxtArea.setCaretPosition(0);
        jScrollPane1.setViewportView(sendTxtArea);

        sendBtn.setText("Send");
        sendBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendBtnActionPerformed(evt);
            }
        });

        buzzBtn.setText("Buzz");
        buzzBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buzzBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout sendPanelLayout = new javax.swing.GroupLayout(sendPanel);
        sendPanel.setLayout(sendPanelLayout);
        sendPanelLayout.setHorizontalGroup(
            sendPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(sendPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(sendPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buzzBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
                    .addComponent(sendBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE))
                .addContainerGap())
        );
        sendPanelLayout.setVerticalGroup(
            sendPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(sendPanelLayout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(sendBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(buzzBtn)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(statusLbl))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                            .addComponent(sendPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusLbl)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sendPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void sendBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendBtnActionPerformed
        // TODO add your handling code here:
        doSendMessage();
    }//GEN-LAST:event_sendBtnActionPerformed

    private void buzzBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buzzBtnActionPerformed
        // TODO add your handling code here:
        
        System.out.println("Attempting to send message");
        String messageTo = this.receiver;
        String messageText = "BUZZ!!!";
        try
        {
            this.imclient.getYmSession().sendBuzz(messageTo);
            updateChatArea(this.sessionHost + ": " + messageText + '\n');
            updateSendArea();
            
            if (this.imclient.getHostContacts().contactExists(messageTo) == false)
                this.imclient.getHostContacts().getOnContact().put(messageTo, this);
            
        } catch (IllegalStateException e1) {
            System.out.println("[Send Message Error] : IllegalStateException\n"+e1.getMessage());
            e1.printStackTrace();
        } catch (IOException e1) {
            System.out.println("[Send Message Error] : IOException\n"+e1.getMessage());
            e1.printStackTrace();
        } catch (Exception e) {
            System.out.println("[Send Message Error] : "+e.getMessage());
            e.printStackTrace();
        }
    }//GEN-LAST:event_buzzBtnActionPerformed
    
    private void doSendMessage()
    {
        System.out.println("Attempting to send message");
        String messageTo = this.receiver;
        String messageText = this.sendTxtArea.getText();
        
        if (messageText.equals(""))
        {
            System.out.println("Message Empty!");
        }
        else    {
            try 
            {
                this.imclient.getYmSession().sendMessage(messageTo, messageText);
                updateChatArea(this.sessionHost + ": " + messageText + '\n');
                updateSendArea();

                if (this.imclient.getHostContacts().contactExists(messageTo) == false)
                    this.imclient.getHostContacts().getOnContact().put(messageTo, this);

            } catch (IllegalStateException e1) {
                System.out.println("[Send Message Error] : IllegalStateException\n"+e1.getMessage());
                e1.printStackTrace();
            } catch (IOException e1) {
                System.out.println("[Send Message Error] : IOException\n"+e1.getMessage());
                e1.printStackTrace();
            } catch (Exception e) {
                System.out.println("[Send Message Error] : "+e.getMessage());
                e.printStackTrace();
            }
        }//else
    }
    
    public void updateChatArea(String text)
    {
        int length = this.chatTxtArea.getText().length();
        
        this.chatTxtArea.append(text);
        this.chatTxtArea.setCaretPosition(length);
    }
    
    public void updateSendArea()
    {
        this.sendTxtArea.setCaretPosition(0);
        this.sendTxtArea.setText("");
        
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buzzBtn;
    private javax.swing.JTextArea chatTxtArea;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton sendBtn;
    private javax.swing.JPanel sendPanel;
    private javax.swing.JTextArea sendTxtArea;
    private javax.swing.JLabel statusLbl;
    // End of variables declaration//GEN-END:variables

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public javax.swing.JTextArea getChatTxtArea() {
        return chatTxtArea;
    }

    public void setChatTxtArea(javax.swing.JTextArea chatTxtArea) {
        this.chatTxtArea = chatTxtArea;
    }

    public javax.swing.JTextArea getSendTxtArea() {
        return sendTxtArea;
    }

    public void setSendTxtArea(javax.swing.JTextArea sendTxtArea) {
        this.sendTxtArea = sendTxtArea;
    }

    public javax.swing.JLabel getStatusLbl() {
        return statusLbl;
    }

    public void setStatusLbl(javax.swing.JLabel statusLbl) {
        this.statusLbl = statusLbl;
    }
    
}
